import java.util.Random;
//使用一个类实现 累加方法
//sum运算符
class SumOperator{
    //定义两个变量分别记录奇数、偶数下标的和
    long evenSum;//偶数和
    long oddSum;//奇数和

    public void addEvenSum(int n){
        evenSum += n;
    }

    public void addOddSum(int n){
        oddSum += n;
    }

    public long getEvenSum() {
        return evenSum;
    }

    public void setEvenSum(long evenSum) {
        this.evenSum = evenSum;
    }

    public long getOddSum() {
        return oddSum;
    }

    public void setOddSum(long oddSum) {
        this.oddSum = oddSum;
    }

    public long result() {
        return oddSum + evenSum;
    }
}

public class Test02 {
    public static final int ARRAY_SIZE = 1000_0000;
    public static final int SIZE_MAX = 100;
    public static void main(String[] args) throws InterruptedException {
        //开始时间
        long start = System.currentTimeMillis();
        //数组
        int[] array = new int[ARRAY_SIZE];
        //构造随机数,填充数组
        Random random = new Random();
        for (int i = 0; i < ARRAY_SIZE; i++) {
            array[i] = random.nextInt(SIZE_MAX) + 1;
        }

        //实例化对象
        SumOperator operator = new SumOperator();


        //创建线程一，计算偶数下标和
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < ARRAY_SIZE; i += 2) {
                operator.addEvenSum(array[i]);
            }


        });

        //创建线程二，计算奇数下标和
        Thread t2 = new Thread(() -> {

            for (int i = 1; i < ARRAY_SIZE; i += 2) {
                operator.addOddSum(array[i]);
            }
        });

        //启动线程
        t1.start();
        t2.start();
        //等待线程结束
        t1.join();
        t2.join();


        //结束时间
        long end = System.currentTimeMillis();
        System.out.println("偶数和：" + operator.getEvenSum());
        System.out.println("奇数和：" + operator.getOddSum());
        System.out.println("总和：" + operator.result());
        System.out.println("总耗时：" + (end - start) + "ms.");

    }
}